package com.hs.telepass.controller.task;


import android.content.Intent;

import com.hs.telepass.controller.TaskController;
import com.hs.telepass.manager.PreferencesManager;
import com.hs.telepass.model.json.response.GetOrgInfoResponseJson;
import com.hs.telepass.model.json.response.SetDeviceTokenResponseJson;
import com.hs.telepass.utils.JsonUtil;

public class SetDeviceTokenTask extends Task {
    public final static String ACTION_SET_DEVICE_TOKEN = "action_set_device_token";

    private String jsonRequstStr;
    private String taskUrl;
    private int requestType;
    private int storeId;

    public SetDeviceTokenTask(int store_id, String last_five_number, String device_model, String token) {
        jsonRequstStr = "store_id=" + store_id + "&last_five_number="
                + last_five_number + "&device_model=" + device_model
                + "&token=" + token;

        requestType = TASK_REQUST_TYPE_POST;
        taskUrl = BASE_ORG_API_URL + "/" + "ApiPairUser/setDeviceToken";
        this.storeId = store_id;
    }

    @Override
    public void onExecuted(String response, int httpCode) {
        Intent intent = new Intent(getActionName());

        SetDeviceTokenResponseJson json = JsonUtil.fromJson(response, SetDeviceTokenResponseJson.class);

        int status = json.getStatus();
        if (0 != status && 3!= status) {
            intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_ERROR);
            intent.putExtra(TaskController.EXTRA_RESPONSE, "服务器返回" + status + "状态码");
        } else {
            intent.putExtra(TaskController.EXTRA_EVENT, TaskController.EVENT_TASK_SUCCEED);

            //保持 store_id
            PreferencesManager.getInstance().setStoreId(storeId);
        }


        broadcastManager.sendBroadcast(intent);
    }

    @Override
    public int getRequstType() {
        return requestType;
    }

    @Override
    public String getJsonRequstStr() {
        return jsonRequstStr;
    }

    @Override
    public int getResponseSuccessCode() {
        return 0;
    }

    @Override
    public String getTaskUrl() {
        return taskUrl;
    }

    @Override
    public String getActionName() {
        return ACTION_SET_DEVICE_TOKEN;
    }

}
